package com.example.seckill.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

@Component
public class MD5Utils {

    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    public static final String salt = "1a2b3c4d";

    //MD5加密算法,第一次加密，从用户端传到服务端
    public static String inputPassToFormPass(String inputPass) {
        String str = "" +  salt.charAt(0)+salt.charAt(2)+inputPass+salt.charAt(5)+salt.charAt(4);
        return md5(str);
    }

    //MD5加密算法，第二次加密，从服务端传到数据库
    public static String fromPassToDBPass(String formPass,String salt) {
        String str = "" + salt.charAt(0)+salt.charAt(2)+formPass+salt.charAt(5)+salt.charAt(4);
        return md5(str);
    }

    //两次加密的组合，真正的加密过程，从用户端传这个
    public static String inputPassToDBPass(String inputPass,String salt) {
        String fromPass = inputPassToFormPass(inputPass);
        String dbPass = fromPassToDBPass(fromPass,salt);
        return dbPass;
    }

    //测试
    public static void main(String[] args) {
        System.out.println(inputPassToFormPass("123456"));
        System.out.println(fromPassToDBPass("b7797cce01b4b131b433b6acf4add449","1a2b3c4d"));
        System.out.println(inputPassToDBPass("123456","1a2b3c4d"));
    }
}
